(function() {
    angular.module('app').component('darenShopComponent', {
        templateUrl: 'app/controllers/redNet.controller/daren.shop.component/daren.shop.component.html',
        controller: darenShopComponent,
        controllerAs: 'vm'
    });

    //tableBrandShopsService 来自components->table.list.component->table.shop.component
    darenShopComponent.$inject = ['darenShopComponentService', '$loading', '$rootScope', '$scope', 'tableBrandShopsService', 'customPaginationService'];

    function darenShopComponent(darenShopComponentService, $loading, $rootScope, $scope, tableBrandShopsService, customPaginationService) {

        var vm = this;

        vm.$onInit = function() {
            // console.log('darenShopComponentService is loading');
            vm.pagination = customPaginationService.get();


        }

        vm.filter = {
            sortColumn: 'sell_money',

            darenUid: '',

            sort: 'down'
        }



        vm.daren = {};

        vm.table = {
            count: 0,
            shops: []
        }


        //获取店铺详细监控
        vm.getShopBasic = tableBrandShopsService.getShopBasic; //function

        //获取店铺中的商品
        //发送广播分别到table.list.component和table.shop.prods
        vm.getShopProds = _getShopProds; //function

        vm.pageChanged = _pageChanged; //function

        //$on
        var clearFun = $rootScope.$on('redNet_daren_shop_EV', function(e, daren) {

            vm.daren = daren;

            vm.filter.darenUid = daren.darenUid;

            
            vm.pageChanged(1);


            // console.log('-----------------------');

            // console.log(vm.pagination);

            // _init();

        });

        $scope.$on('$destroy', function() {
            clearFun();
        });


        function _init() {

            // console.log('为什么请求2次');

            $loading.start('darenShop');

            // var json = angular.copy(vm.filter);
            var json = angular.extend({}, vm.filter, vm.pagination);

            tableBrandShopsService.shopBasicInit();

            darenShopComponentService.getDarenShop(json).then(function(data) {

                vm.table.count = data.count / 10 * 11;

                vm.table.shops = data.darenShop;

                angular.forEach(vm.table.shops, function(shop, index) {

                    if (shop.sellAmount == 0) {

                        shop.avgPrice = 0;

                    } else {
                        shop.avgPrice = (shop.sellMoney / shop.sellAmount).toFixed(2);
                    }

                    shop.userId = shop.uid;

                });

                $loading.finish('darenShop');

            }, function(err) {});
        }

        function _pageChanged(newPageNumber) {

            // console.log('分页页数点击；了' + newPageNumber);

            customPaginationService.pageChange(vm.pagination, newPageNumber);
            // vm.filter.newPageNumber = newPageNumber;
            // vm.filter.begin = (vm.filter.newPageNumber - 1) * vm.filter.count;
            // console.log('this.is loading page');
            _init();
        }


        function _getShopProds(shop) {
            $rootScope.$emit('formShop_detail_EV', shop.userId, shop.shopName);
        }


    }
})();
